﻿namespace DotNetUtility.IO
{
    using System.Configuration;
    using DotNetUtility.ExtensionMethods;

    /// <summary>
    /// To read entry from the config file.
    /// </summary>
    public static class ConfigurationReader
    {
        /// <summary>
        /// To read an appsettings entry from the config file. Entry will be idetified by key name.
        /// </summary>
        /// <param name="configKey">The name of the key</param>
        /// <returns>The value matched with key name.</returns>
        /// <example>
        ///     <code>
        ///         string configKey="MyConfigKey"
        ///         string keyValue = ConfigurationReader.ReadValueOfConfigKey(configKey)
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static string ReadValueOfConfigKey(string configKey)
        {
            return ConfigurationManager.AppSettings[configKey];
        }

        /// <summary>
        /// To read the connectionstring value from the config file.
        /// </summary>
        /// <param name="connectionStringName">The name of the connection string.</param>
        /// <returns>The value of the connection string.</returns>
        /// <example>
        ///     <code>
        ///         string connectionStringsName="MyConnectionStringName"
        ///         string connectionStringValue = ConfigurationReader.ReadConnectionStringOf(connectionStringsName)
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static string ReadConnectionStringOf(string connectionStringName)
        {
            var connectionStringCollection = ConfigurationManager.ConnectionStrings[connectionStringName];
            return connectionStringCollection.IsNotEqualToNull() ? connectionStringCollection.ConnectionString : default(string);
        }
    }
}